package com.lifang.imgsoa.qiniu;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import lombok.Data;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.util.StringMap;

@Data
@Resource
public class QNUtil
{

    private File file;
    private String key;

    private Logger log = LoggerFactory.getLogger(this.getClass());

    private void upload() throws Exception
    {
        Response res = UploadManagerFactory.initUploadManager().put(getFile(), key, TokenFactory.initFactory().getUpToken0());
        MyRet ret = res.jsonToObject(MyRet.class);
        log.info(res.toString());
        log.info(res.bodyString());
    }

    public void upload1() throws Exception
    {
        Response res = UploadManagerFactory.initUploadManager().put(getFile(), key, TokenFactory.initFactory().getUpToken0());
        Map m2 = res.jsonToObject(Map.class);
        Map<String, Object> m3 = res.jsonToObject(Map.class);
        Map<String, Object> m4 = res.jsonToObject(HashMap.class);
        StringMap m1 = res.jsonToMap();
    }

    // 上传内存中数据
    public MyRet upload(byte[] data, String key) throws Exception
    {
        Response res = UploadManagerFactory.initUploadManager().put(data, getKey(), TokenFactory.initFactory().getUpToken1(key));
        MyRet ret = res.jsonToObject(MyRet.class);
        return ret;
    }

    public void uploadFile() throws Exception
    {
        Response res = UploadManagerFactory.initUploadManager().put(getFile(), getKey(), TokenFactory.initFactory().getUpToken0());
        // log.info(res);
        System.out.println(res);
        // log.info(res.bodyString());
        // Ret ret = res.jsonToObject(Ret.class);
    }

    public static void main(String[] args)
    {
        QNUtil util = new QNUtil();
        File file = new File("D:/1.png");
        util.setFile(file);
        util.setKey("1/2/3/4/5/1.png");
        try
        {
            util.uploadFile();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

}
